<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录页面</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
        .box {
            width: 1000px;
            margin: auto;
        }
        
        .wrap {
            width: 600px;
            height: 200px;
            background: rgb(202, 199, 199);
            margin: auto;
            padding: 20px 70px 20px 50px;
        }
        
        #check {
            margin-left: 45px;
            margin-right: 20px;
        }
        
        button {
            margin-left: 40px;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>用户登录系统</h2>
        <hr>
        <div class="form-horizontal wrap">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="username" placeholder="请输入用户名">
                </div>
            </div>

            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="password" placeholder="请输入密码">
                </div>
            </div>

            <div class="form-group">
                <input type="checkBox" id="check">30天内免登录<br>
                <button class="btn btn-primary" id="reload">登录</button>
            </div>
        </div>
    </div>

    <script src="lib/jquery.min.js"></script>
    <script src="lib/common.js"></script>

    <script>
        $("#reload").click(function() {
            let username = $("#username").val();
            let password = $("#password").val();

            //登录请求
            if (username && password) {
                $.ajax({
                    url: `${address}/login`,
                    method: "post",
                    data: {
                        username,
                        password
                    },
                    success(data) {
                        if (data === "2") {
                            alert("用户名或密码错误")
                        } else {
                            let newData = JSON.stringify(data);
                            //是否记住30天内免登录
                            if ($("#check")[0].checked) {
                                let date = new Date();
                                date.setDate(date.getDate() + 30);
                                document.cookie = `userInfo=${newData};expires=${date}`;
                            }
                            sessionStorage.setItem("userInfo", newData);
                            location.href = `${address}/studentlist.html`;
                        }
                    }
                })
            }
        })
    </script>
</body>

</html>